<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label=" 车辆类型" prop="carType">
        <el-select v-model="queryParams.carType" placeholder="请选择 车辆类型" clearable>
          <el-option
            v-for="dict in dict.type.car_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label=" 启用状态" prop="state">-->
<!--        <el-select v-model="queryParams.state" placeholder="请选择 启用状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.state"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="审核状态" prop="checkState">-->
<!--        <el-select v-model="queryParams.checkState" placeholder="请选择审核状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.check_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="拉黑状态" prop="blockState">-->
<!--        <el-select v-model="queryParams.blockState" placeholder="请选择拉黑状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.block_state"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cars:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cars:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cars:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleCheckStateChange"

        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:cars:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carsList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="showStatusColumn"/>
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label=" 车辆类型" align="center" prop="carType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_type" :value="scope.row.carType"/>
        </template>
      </el-table-column>
      <el-table-column label=" 司机" align="center" prop="driver" />
      <el-table-column label=" 车牌颜色" align="center" prop="carnoColor">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.carno_color" :value="scope.row.carnoColor"/>
        </template>
      </el-table-column>
<!--      <el-table-column label=" 启用状态" align="center" prop="state">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.state" :value="scope.row.state"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="审核状态" align="center" prop="checkState">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.check_status" :value="scope.row.checkState"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="拉黑状态" align="center" prop="blockState">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.block_state" :value="scope.row.blockState"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label=" 审核人" align="center" prop="reviewer" />-->
<!--      <el-table-column label=" 审核时间" align="center" prop="reviewTime" />-->
      <el-table-column label=" 创建用户名" align="center" prop="createUserName" />
      <el-table-column label=" 创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cars:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cars:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label=" 车辆类型" prop="carType">
          <el-radio-group v-model="form.carType">
            <el-radio
              v-for="dict in dict.type.car_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label=" 车牌颜色" prop="carnoColor">
          <el-radio-group v-model="form.carnoColor">
            <el-radio
              v-for="dict in dict.type.carno_color"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="司机" prop="driverId">
          <el-select v-model="form.driverId" placeholder="请选择司机" clearable @change="handleDriverChange">
            <el-option
              v-for="driver in this.driverList"
              :key="driver.id"
              :label="driver.name"
              :value="driver.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCars, getCars, delCars, addCars, updateCars,checkStateChange } from "@/api/system/cars"
import {getDriverInfoList } from "@/api/system/drivers"

export default {
  name: "Cars",
  dicts: ['check_status', 'block_state', 'carno_color', 'car_type', 'state'],
  data() {
    return {
      showStatusColumn:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆表格数据
      carsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //  车牌颜色时间范围
      daterangeCreateTime: [],

      paramName:"",

      driverList: [], // 司机列表
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        licensePlate: null,
        carType: null,
        driver: null,
        state: null,
        checkState: null,
        blockState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        licensePlate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        carType: [
          { required: true, message: " 车辆类型不能为空", trigger: "change" }
        ],
        driverId: [
          { required: true, message: "司机不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getDriverList(""); // 初始化时加载司机列表
  },
  methods: {

    handleDriverChange(param){
      this.getDriverList(param);
    },

    /** 获取司机列表 */
    getDriverList(param) {
      // 这里需要替换为实际的API调用
      getDriverInfoList(param).then(response => {
        this.driverList = response.rows
      })
    },


      /** 查询车辆列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0]
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1]
      }
      listCars(this.queryParams).then(response => {
        this.carsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        updateTime: null,
        createUserId: null,
        createOrgId: null,
        updateUserId: null,
        updateUserName: null,
        isDelete: null,
        orgId: null,
        createOrgName: null,
        sqare01: null,
        sqare02: null,
        sqare03: null,
        sqare04: null,
        sqare05: null,
        sqare06: null,
        licensePlate: null,
        carType: null,
        driver: null,
        driverId: null,
        carnoColor: null,
        state: null,
        checkState: null,
        blockState: null,
        reviewer: null,
        reviewTime: null,
        createUserName: null,
        createTime: null,
        contactId: null,
        notPresentCount: null,
        needQueueup: null,
        reviewerId: null,
        reviewbyOrg: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      // this.getDriverList() // 获取司机列表
      this.title = "添加车辆"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      // this.getDriverList() // 获取司机列表
      const id = row.id || this.ids
      getCars(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改车辆"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCars(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCars(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const licensePlate = row.licensePlate
      const idss = row.id
      const newids = this.ids
      let message = ""
      if(licensePlate != null)
        message = '是否确认删除车辆号为"' + licensePlate + '"的数据项？'
      else if (newids != null)
        message = '是否确认删除选中所有数据项？'
      else
        message = '请选择数据项'
      this.$modal.confirm(message)
      .then(function() {
        return delCars(idss)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

    handleCheckStateChange() {
      const ids = this.ids
      this.$modal.confirm('是否发起审核?').then(function() {
        return checkStateChange(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("审核中")
      }).catch(() => {})
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cars/export', {
        ...this.queryParams
      }, `cars_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
